/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs;

import org.digitalsoul.switchplugin.patterns.CompoundPattern;
import org.digitalsoul.switchplugin.patterns.FileExtensionPattern;
import org.digitalsoul.switchplugin.patterns.FileLocationPattern;
import org.digitalsoul.switchplugin.patterns.FilenameSuffixPattern;
import org.digitalsoul.switchplugin.patterns.IPattern;
import org.digitalsoul.switchplugin.patterns.NamedCompoundPattern;
import org.digitalsoul.switchplugin.patterns.PatternType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * Class PatternConfiguration
 */
public class PatternConfigurator {

    /**
     * 
     */
    private final Collection<NamedCompoundPattern> patternConfigs;
    
    /**
     * 
     */
    private CompoundPattern currentPattern;
    
    /**
     * @param patterns 
     * 
     */
    public PatternConfigurator(Collection<NamedCompoundPattern> patterns) {
        patternConfigs = new ArrayList<NamedCompoundPattern>();
        patternConfigs.addAll(patterns);
        currentPattern = patternConfigs.iterator().next();
    }
    
    public void addSubPattern(String selectedValue, String value) {
        IPattern newPattern = null;
        if (selectedValue.equals(PatternType.FILE_LOCATION.toString())) {
            newPattern = new FileLocationPattern(value);
        }
        else if (selectedValue.equals(PatternType.FILENAME_EXTENSION.toString())) {
            newPattern = new FileExtensionPattern(value);
        }
        else if (selectedValue.equals(PatternType.FILENAME_SUFFIX.toString())) {
            newPattern = new FilenameSuffixPattern(value);
        }
        if (!hasSubPatternType(newPattern)) {
            currentPattern.add(newPattern);
        }
    }
    
    private boolean hasSubPatternType(IPattern newPattern) {
        boolean hasSubPattern = false;
        for (IPattern pat : getSubPatterns()) {
         if (pat.getType() == newPattern.getType()) {
             hasSubPattern = true;
         }
        }
        return hasSubPattern;
    }

    public void removeSubPattern(IPattern pattern) {
        currentPattern.getPatterns().remove(pattern);
    }
    

    public void removeSubPattern(int index) {
        currentPattern.getPatterns().remove(index);
    }
    
    public void selectCurrentPattern(String patternName) {
        for (NamedCompoundPattern pat : patternConfigs) {
            if (pat.getName().equals(patternName)) {
                this.currentPattern = pat;
                break;
            }
        }
    }

    public List<IPattern> getSubPatterns() {
        return currentPattern.getPatterns();
    }
    
    public Collection<NamedCompoundPattern> getPatterns() {
        return patternConfigs;
    }

    public void removeSubPattern(String patternDescription) {
        List<IPattern> patternsToDelete = new ArrayList<IPattern>();
        for (IPattern pat : getSubPatterns()) {
            if (pat.toString().equals(patternDescription)) {
                patternsToDelete.add(pat);
            }
        }
        for (IPattern pat : patternsToDelete) {
            removeSubPattern(pat);
        }
    }
}
 